package com.zx.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zx.entity.EUser;
import com.zx.entity.ref.ResponseResult;
import com.zx.service.IEUserService;
import org.apache.catalina.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhouxin
 * @since 2020-06-10
 */
@CrossOrigin
@RestController
@RequestMapping("user")
public class EUserController {

    @Autowired
    private IEUserService userService;

    @PostMapping("login")
    public ResponseResult login(@RequestBody EUser user){
        ResponseResult result = ResponseResult.SUCCESS();
        try {
            QueryWrapper queryWrapper = new QueryWrapper();
            if (user.getUsername()!=null){
                queryWrapper.eq("username",user.getUsername());
            }
            if (user.getPassword()!=null){
                queryWrapper.eq("password",user.getPassword());
            }
            List<EUser> list = userService.list(queryWrapper);
            if (list.size()>0){
                //查询成功，及登录成功
                EUser user1 = list.get(0);
                if (user1.getRole().equals("0")){
                    //为销售端发送给前端进行不同页面跳转
                    result.setMessage("0");
                    result.setResult(user1.getId());
                }
                if (user1.getRole().equals("1")){
                    //为经理端
                    result.setMessage("1");
                    result.setResult(user1.getId());
                }
            }else{
                //登录失败 密码或者用户名不正确
                result.setSuccess(false);
                result.setMessage("登录失败!");
            }
        }catch (Exception e){
            result.setSuccess(false);
            result.setMessage("登录失败！"+e.toString());
        }
        return result;
    }

}
